const router = require('koa-router')();

const { env } = require('../../libs');

const unSupportHarborMessage = '当前环境不支持Harbor！';

const options = {
  Headers: {
    Authorization: env.harbor.authorization
  }
};

router.prefix('/api/harbor');

router.get('/:project/:repository', async (ctx, next) => {
  let rs = ctx.getResponse();

  if (!env.harbor.enable) {
    rs.success = false;
    rs.message = unSupportHarborMessage;
  } else {
    let project = ctx.params.project;
    let repository = encodeURI(ctx.params.repository);

    let response = await ctx.httpClient.get(`${env.harbor.apiPrefix}/projects/${project}/repositories/${repository}/artifacts?with_tag=true&with_scan_overview=false&with_label=true&page_size=100&page=1`, options);

    rs.datas = JSON.parse(response.body);
  }

  ctx.body = rs;
});

module.exports = router;